﻿
///**********************************************************************************普通用的拼音查找
(function (ns, $, ko) {

    ZYCLib.RegNameSpace(ns).CodexSearch = (function () {

        var _CallBackCodexName = null;//点击药材的回调函数
        var _CallBackPage = null;//分页部分函数
        var _LetterUrl = "";//拼音查找地址 
        var _LetterTarget = "_blank";//拼音.1本页跳转，2弹出页面(默认)
        var _IsOpen = false;//区域折叠好的

        var InitLetterSearch = function(callBackCodexName, callBackPage, url, targetType) {

            _CallBackCodexName = callBackCodexName;
            _CallBackPage = callBackPage;

            if (_LetterUrl == "" && url != null && url != undefined && url != "") {
                _LetterUrl = url;
            }

            if (targetType == 1) {
                _LetterTarget = "_self";
            }

            $("#btnDelCodex").click(btnDelCodex_Click);
            $("#btnDelPlace").click(btnDelPlace_Click);
            $("#btnDelKeyword").click(btnDelKeyword_Click);


            GetAllCity();
        };

        var IsNullObj = function(obj) {

            if (obj == null || obj == undefined || obj == "") {
                return true;
            } else {
                return false;
            }
        };
        var btnDelKeyword_Click = function () {
            $("#lblKey").html("");
            $("#keytag").css("display", "none");

            if (_CallBackPage != null) {
                _CallBackPage();
            }

            if (_CallBackCodexName != null) {
                _CallBackCodexName();
            }

        };

        var btnDelCodex_Click = function() {
            $("#lblCodex").html("");
            $("#lblCodex").attr("codexid", "0");
            $("#pztag").css("display", "none");

            if (_CallBackPage != null) {
                _CallBackPage();
            }

            if (_CallBackCodexName != null) {
                _CallBackCodexName();
            }

        };

        var btnDelPlace_Click = function() {
            $("#lblPlace").html("");
            $("#lblPlace").attr("placeid", "0");
            $("#ystag").css("display", "none");

            if (_CallBackPage != null) {
                _CallBackPage();
            }

            if (_CallBackCodexName != null) {
                _CallBackCodexName();
            }

        };

        var PlaceClick = function(placeId, placeName) {
            $("#lblPlace").html(placeName);
            $("#lblPlace").attr("placeid", placeId);
            $("#ystag").css("display", "block");

            if (_CallBackPage != null) {
                _CallBackPage();
            }

            if (_CallBackCodexName != null) {
                _CallBackCodexName();
            }
        };

        var btnOpenClose_Click = function(placeId, placeName) {
            if (_IsOpen) {
                _IsOpen = false;
                $("#btnOpenClose").html('↓展开');
                $('#pro').animate({ 'height': '25px' }, 200);
            } else {
                _IsOpen = true;

                $("#btnOpenClose").html('↑收起');
                $('#pro').animate({ 'height': '125px' }, 200);
            }
        };


        var GetCodexLetter = function(letter) {

            ZYCLib.Web.JsonAjax.PostData("common/MCodexService/GetCodexLetter", { letter: letter }, function(ret) {

                var letterHtml = "";

                for (var i = 0; i < ret.ListLetter.length; i++) {
                    var eachLetter = ret.ListLetter[i];

                    if (i == 0) {
                        letterHtml += " <a href=\"javascript:void(0);\" sign='subletter' onclick=\"ZYCLib.WebPage.CodexSearch.GetCodexNameByLetter('" + eachLetter + "');\"  class=\"selected\" >" + eachLetter + "</a> ";
                    } else {
                        letterHtml += " <a href=\"javascript:void(0);\" sign='subletter' onclick=\"ZYCLib.WebPage.CodexSearch.GetCodexNameByLetter('" + eachLetter + "');\">" + eachLetter + "</a> ";
                    }


                }

                $("#divMCodexSubLetters").html(letterHtml);

            }, function() {
            });

        };

        var GetCodexNameByLetter = function(letter) {
            ZYCLib.Web.JsonAjax.PostData("common/MCodexService/GetCodexNameByLetter", { letter: letter, url: _LetterUrl }, function(ret) {

                var codexHtml = "";

                for (var i = 0; i < ret.length; i++) {
                    var eachCodex = ret[i];

                    if (i == 0) {
                        codexHtml += "<a  href=\"javascript:void(0);\" sign='codexname'  codexid=\"" + eachCodex.MBID + "\"  onclick='ZYCLib.WebPage.CodexSearch.CodexNameClick(this)'  class=\"selected\">" + eachCodex.MName + "</a>  ";
                    } else {
                        codexHtml += "<a  href=\"javascript:void(0);\" sign='codexname'  codexid=\"" + eachCodex.MBID + "\"  onclick='ZYCLib.WebPage.CodexSearch.CodexNameClick(this)' >" + eachCodex.MName + "</a>  ";
                    }

                }

                $("#divPinyinMCodex").html(codexHtml);

            }, function() {
                $("#divMCodexSubLetters").find(".selected").attr("class", "");
                $.each($("#divMCodexSubLetters").children(), function(i, item) {
                    if (item.innerHTML == letter) {
                        $(item).attr("class", "selected");
                    }
                });
                if ($("#divMCodexSubLetters").find(".selected").length == 0) {
                    $($("#divMCodexSubLetters").children()[0]).attr("class", "selected");
                }
            });

        };

        var GetAllCity = function(letter) {
            ZYCLib.Web.JsonAjax.PostData("common/BasePCAService/GetAllCitys", {}, function(ret) {

                var areaHtml = "";

                for (var i = 0; i < ret.length; i++) {
                    var eachArea = ret[i];

                    areaHtml += "   <a href=\"javascript:void(0);\" onclick=\"ZYCLib.WebPage.CodexSearch.PlaceClick(" + eachArea.PCAID + ", '" + eachArea.PCAName + "')\">" + eachArea.PCAName + "</a> ";

                }

                areaHtml += " <i id=\"btnOpenClose\">↓展开</i>";

                $("#pro").html(areaHtml);

                $("#btnOpenClose").click(btnOpenClose_Click);

            }, function() {
            });

        };

        var GetSubLetter = function(obj, letter) {

            $(obj).siblings().removeAttr("class");
            $(obj).attr("class", "selected");

            $("#divMCodexSubLetters").css("display", "block");
            $("#divPinyinMCodex").css("display", "block");

            GetCodexLetter(letter);
            GetCodexNameByLetter(letter);
        };

        var CodexNameClick = function(obj) {

            $("#lblCodex").html($(obj).html());
            $("#lblCodex").attr("codexid", $(obj).attr("codexid"));
            $("#pztag").css("display", "block");

            $("#divMCodexSubLetters").css("display", "none");
            $("#divPinyinMCodex").css("display", "none");
            $(".pyTop").children(".selected").toggleClass("selected");

            if (_CallBackPage != null) {
                _CallBackPage();
            }

            if (_CallBackCodexName != null) {
                _CallBackCodexName();
            }
        };


        return {
            InitLetterSearch: InitLetterSearch,
            GetSubLetter: GetSubLetter,
            GetCodexNameByLetter: GetCodexNameByLetter,
            CodexNameClick: CodexNameClick,
            PlaceClick: PlaceClick,
        };


    })();

})("WebPage", jQuery, ko);


function GetSubLetter(obj, letter)
{
    ZYCLib.WebPage.CodexSearch.GetSubLetter(obj, letter);
}

function PlaceClick(placeId, placeName) {
    ZYCLib.WebPage.CodexSearch.PlaceClick(placeId, placeName);
}






